#include "Preprocessor.hpp"
#include "Tokenizer.hpp"

// Ctor & Dtor
Tokenizer::Tokenizer(const std::string& inputString)
: inputString_(Preprocessor::Preprocess(inputString))
, seekPointer_(inputString_.begin()) {
	buildMaps();
	buildTokenQueue();
}


Tokenizer::Tokenizer(const Tokenizer& cpyTokenizer)
: inputString_(cpyTokenizer.inputString_)
, seekPointer_(inputString_.begin()) {}


// Operateurs
Tokenizer Tokenizer::operator =(const Tokenizer& cpyTokenizer) {
	inputString_ = cpyTokenizer.inputString_;
	seekPointer_ = inputString_.begin(); 

	return *this;
}

// Méthodes publiques
bool Tokenizer::isAtBegin() const { return seekPointer_ == inputString_.begin(); }
bool Tokenizer::isAtEnd() const { return tokens_.empty(); }

void Tokenizer::buildMaps()
{
	charStates[' '] = FREE;
	charStates['['] = SET;
	charStates['{'] = NOSET;
	
	charModifiers[' '] = NONE;
	charModifiers['*'] = STAR;
	charModifiers['+'] = PLUS;
	charModifiers['?'] = QUESTION_MARK;
	charModifiers['^'] = START;
	charModifiers['$'] = END;
}

void Tokenizer::buildTokenQueue()
{
	while(seekPointer_ != inputString_.end())
	{
		Token tok;
		tok.State = charStates[static_cast<char>(*(seekPointer_++))];
		tok.Modifier = charModifiers[static_cast<char>(*(seekPointer_++))];
		tok.Valid = static_cast<char>(*(seekPointer_++));
		seekPointer_++;
		tokens_.push(tok);
	}
}


Token Tokenizer::getNextToken() { 
	Token tmp = tokens_.front();
	tokens_.pop();
	return tmp;
}

